package cn.iocoder.yudao.module.erp.dal.mysql.dp;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpyw.vo.DpYwPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.List;

/**
 * ERP义乌制单 Mapper
 * @author 天使管理员
 */
@Mapper
public interface DpYwMapper extends BaseMapperX<DpYwDO> {

    default PageResult<DpYwDO> selectPage(DpYwPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpYwDO>()
                .likeIfPresent(DpYwDO::getId, reqVO.getId())
                .likeIfPresent(DpYwDO::getNo, reqVO.getNo())
                .eqIfPresent(DpYwDO::getStoreType, reqVO.getStoreType())
                .eqIfPresent(DpYwDO::getInCount, reqVO.getInCount())
                .likeIfPresent(DpYwDO::getInName, reqVO.getInName())
                .eqIfPresent(DpYwDO::getInVolume, reqVO.getInVolume())
                .eqIfPresent(DpYwDO::getWeight, reqVO.getWeight())
                .eqIfPresent(DpYwDO::getDensity, reqVO.getDensity())
                .eqIfPresent(DpYwDO::getDeliveryPhone, reqVO.getDeliveryPhone())
                .likeIfPresent(DpYwDO::getDeliveryName, reqVO.getDeliveryName())
                .eqIfPresent(DpYwDO::getEarpayAmount, reqVO.getEarpayAmount())
                .eqIfPresent(DpYwDO::getInPic, reqVO.getInPic())
                .eqIfPresent(DpYwDO::getLine, reqVO.getLine())
                .eqIfPresent(DpYwDO::getFile, reqVO.getFile())
                .eqIfPresent(DpYwDO::getNote, reqVO.getNote())
                .likeIfPresent(DpYwDO::getShipper, reqVO.getShipper())
                .eqIfPresent(DpYwDO::getDestination, reqVO.getDestination())
                .eqIfPresent(DpYwDO::getConsignee, reqVO.getConsignee())
                .eqIfPresent(DpYwDO::getAdvancePayPack, reqVO.getAdvancePayPack())
                .eqIfPresent(DpYwDO::getAdvancePayPackUp, reqVO.getAdvancePayPackUp())
                .eqIfPresent(DpYwDO::getAdvancePayChinaFreight, reqVO.getAdvancePayChinaFreight())
                .eqIfPresent(DpYwDO::getAdvancePayForGoods, reqVO.getAdvancePayForGoods())
                .eqIfPresent(DpYwDO::getCollecting, reqVO.getCollecting())
                .eqIfPresent(DpYwDO::getCustomerRebate, reqVO.getCustomerRebate())
                .eqIfPresent(DpYwDO::getBusinessRebate, reqVO.getBusinessRebate())
                .eqIfPresent(DpYwDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpYwDO::getInventoryStatus, reqVO.getInventoryStatus())
                .eqIfPresent(DpYwDO::getIsSplit, reqVO.getIsSplit())
                .betweenIfPresent(DpYwDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpYwDO::getChooseDate, reqVO.getChooseDateStr())
                .orderByDesc(DpYwDO::getId));
    }

    default PageResult<DpYwDO> getDpYwPage(DpYwPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpYwDO>()
                .likeIfPresent(DpYwDO::getId, reqVO.getId())
                .inIfPresent(DpYwDO::getId,reqVO.getIds())
                .likeIfPresent(DpYwDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpYwDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpYwDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpYwDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpYwDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpYwDO::getLine, reqVO.getLine())
                .likeIfPresent(DpYwDO::getShipper, reqVO.getShipper())
                .inIfPresent(DpYwDO::getStatus,0,1)
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpYwDO::getRemCount, 0)
                .orderByDesc(DpYwDO::getId));
    }

    default PageResult<DpYwDO> selectRemCountNoZeroPage(DpYwPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpYwDO>()
                .likeIfPresent(DpYwDO::getId, reqVO.getId())
                .likeIfPresent(DpYwDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpYwDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpYwDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpYwDO::getLine, reqVO.getLine())
                .eqIfPresent(DpYwDO::getStatus,1)
                //找出入库状态为0或2的
//                .in(DpYwDO::getInventoryStatus, Arrays.asList(0, 2))
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpYwDO::getRemCount, 0)
                .orderByDesc(DpYwDO::getId));
    }

    @Select("<script>" +
            "SELECT temp.id FROM " +
            "(<foreach collection='ids' item='id' separator=' UNION ALL '>" +
            "SELECT #{id} AS id " +
            "</foreach>) temp " +
            "LEFT JOIN erp_dp_yw ON temp.id = erp_dp_yw.id " +
            "WHERE erp_dp_yw.id IS NULL" +
            "</script>")
    List<String> getNotExistIds(@Param("ids") List<String> ids);

}